package com.ocgs.web.action.product;

import com.ocgs.base.core.constant.BaseConstants;
import com.ocgs.base.core.exception.BaseServiceException;
import com.ocgs.base.core.web.action.BaseAction;
import com.ocgs.base.util.DateUtil;
import com.ocgs.model.Product;
import com.ocgs.code.service.impl.CodeServiceImpl;
import com.ocgs.service.impl.ProductServiceImpl;

import java.util.Date;
import java.util.List;


/**
 * @author bryant
 * @version 1.0
 * @since 2012-05-14
 *
 */
public class ProductManageAction extends BaseAction{

    private static final long serialVersionUID = -2484651244734670052L;
    private ProductServiceImpl productService;
    private CodeServiceImpl codeService;
    private List productList;
    private Product product;
    private String[] checkBox;
    public ProductServiceImpl getProductService() {
        return productService;
    }

    public void setProductService(ProductServiceImpl productService) {
        this.productService = productService;
    }

    public CodeServiceImpl getCodeService() {
        return codeService;
    }

    public void setCodeService(CodeServiceImpl codeService) {
        this.codeService = codeService;
    }

    public List getProductList() {
        return productList;
    }

    public void setProductList(List productList) {
        this.productList = productList;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public String[] getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(String[] checkBox) {
        this.checkBox = checkBox;
    }


    public String loadInsertPage() throws BaseServiceException {
        return BaseConstants.ACTION_RESULT_INSERT;
    }

    public String linkUpdatePage() throws BaseServiceException{
        try{
            product =  productService.selectByPrimaryKey(product.getId());
        }catch (BaseServiceException e){
            throw new BaseServiceException(e);
        }
        return BaseConstants.ACTION_RESULT_UPDATE;
    }

    public String loadSearchPage() throws BaseServiceException{

        return BaseConstants.ACTION_RESULT_SEARCH;
    }

    public String search() throws BaseServiceException {
        try {
            paginatedList = productService.findAllProductForPagination(init(getRequest(), paginatedListImpl), product);
        } catch (BaseServiceException e) {
            throw new BaseServiceException(e);
        }
        if(paginatedList==null)
            addActionMessage(this.getText(BaseConstants.MESSAGE_NO_SEARCH_RESULT));
        return BaseConstants.ACTION_RESULT_SEARCH;
    }

    public String insert() throws BaseServiceException {
        try {
            product.setOnCreated(DateUtil.getCurrentTimestamp());
            product.setStatus(BaseConstants.ACTIVE_STATUS);
            productService.insert(product);
        } catch (BaseServiceException e) {
            e.printStackTrace();
            throw e;
        }
        product = new Product();
        addActionMessage(this.getText(BaseConstants.MESSAGE_ADD_SUCCESSFUL));
        return BaseConstants.ACTION_RESULT_INSERT;
    }

    public String update() throws BaseServiceException {
        try {
            product.setLastUpdateTime(new Date());
            productService.updateByPrimaryKeySelective(product);
        } catch (BaseServiceException e) {
            e.printStackTrace();
            throw e;
        }
        addActionMessage(this.getText(BaseConstants.MESSAGE_UPDATE_SUCCESSFUL));
        return BaseConstants.ACTION_RESULT_UPDATE;
    }

    public String delete() throws BaseServiceException {
        try {
            if(checkBox!=null&&checkBox.length!=0){
                Product p = new Product();
                for(String id:checkBox){
                    p.setStatus(BaseConstants.DELETE_STATUS);
                    p.setId(Integer.parseInt(id));
                    productService.updateByPrimaryKeySelective(p);
                }
            }else{
                product.setStatus(BaseConstants.DELETE_STATUS);
                productService.updateByPrimaryKeySelective(product);
            }
            paginatedList = productService.findAllProductForPagination(init(getRequest(), paginatedListImpl), product);
        } catch (BaseServiceException e) {
            e.printStackTrace();
            throw e;
        }
        addActionMessage(this.getText(BaseConstants.MESSAGE_DELETE_SUCCESSFUL));
        return BaseConstants.ACTION_RESULT_SEARCH;
    }
}
